//
//  UIBarButtonItem+HPExtension.swift
//  
//
//  Created by Apeng on 16/4/9.
//
//

import UIKit

extension UIBarButtonItem {
    
    //便利构造函数
    convenience init(imageName :String? = nil, title :String? = nil, target: AnyObject?, action: Selector) {
        self.init()
        
        let btn = UIButton()
        if let img = imageName {
            
            btn.setImage(UIImage(named: img), forState: .Normal)
            btn.setImage(UIImage(named: img+"_highlighted"), forState: .Highlighted)
        }
        
        // 设置文字
        btn.setTitle(title, forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.sizeToFit()
        
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        customView = btn
    }
}
